package com.jsmile.cloud.authc.sys.repo;

import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.authc.api.req.sys.ReqSysDictPageQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysDictQuery;
import com.jsmile.cloud.authc.sys.dao.SysDictionaryDao;
import com.jsmile.cloud.authc.sys.model.SysDictionary;
import com.jsmile.mall.api.kit.DateKit;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年10月16日 02:46:20
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class SysDictionarySearchRepository extends ServiceImpl<SysDictionaryDao, SysDictionary> {

    public List<SysDictionary> findDictByCode(ReqSysDictQuery reqSysDictQuery) {
        return this.baseMapper.findDictByCode(reqSysDictQuery);
    }

    public IPage<SysDictionary> pageQuery(ReqSysDictPageQuery reqSysDictPageQuery) {
        return this.baseMapper.selectPage(reqSysDictPageQuery.getPage(), getQueryWrapper(reqSysDictPageQuery));
    }

    public List<SysDictionary> getList(ReqSysDictQuery reqSysDictQuery) {
        return this.baseMapper.selectList(getQueryWrapper(BeanUtil.toBean(reqSysDictQuery, ReqSysDictPageQuery.class)));
    }

    private Wrapper getQueryWrapper(ReqSysDictPageQuery reqSysDictQuery) {
        return new QueryWrapper<SysDictionary>().lambda().eq(null != reqSysDictQuery.getCurTenantId(), SysDictionary::getTenantId, reqSysDictQuery.getCurTenantId())
            .like(Objects.nonNull(reqSysDictQuery.getDictName()), SysDictionary::getDictName, reqSysDictQuery.getDictName())
            .eq(null != reqSysDictQuery.getParentId(), SysDictionary::getParentId, reqSysDictQuery.getParentId())
            .eq(null != reqSysDictQuery.getLevel(), SysDictionary::getLevel, reqSysDictQuery.getLevel())
            .le(null != reqSysDictQuery.getLeLevel(), SysDictionary::getLevel, reqSysDictQuery.getLeLevel())
            .ge(null != reqSysDictQuery.getGeLevel(), SysDictionary::getLevel, reqSysDictQuery.getGeLevel())
            .eq(null != reqSysDictQuery.getDictCode(), SysDictionary::getDictCode, reqSysDictQuery.getDictCode())
            .eq(null != reqSysDictQuery.getDictValue(), SysDictionary::getDictValue, reqSysDictQuery.getDictValue())
            .eq(null != reqSysDictQuery.getInit(), SysDictionary::getInit, reqSysDictQuery.getInit())
            .eq(null != reqSysDictQuery.getEnable(), SysDictionary::getEnable, reqSysDictQuery.getEnable())
            .eq(null != reqSysDictQuery.getVisible(), SysDictionary::getVisible, reqSysDictQuery.getVisible())
            .ge(null != reqSysDictQuery.getStartDate(), SysDictionary::getCreateTime, DateKit.beginOfDay(reqSysDictQuery.getStartDate()))
            .le(null != reqSysDictQuery.getEndDate(), SysDictionary::getCreateTime, DateKit.endOfDay(reqSysDictQuery.getEndDate())).orderByDesc(SysDictionary::getCreateTime);// sort,createTime倒序
    }
}
